package com;
/*
* 子弹类
* */

import java.awt.*;
import java.util.List;

public class Missile {
    public static final int XSPEED = 10;
    public static final int YSPEED = 10;

    //定制的宽度和高度
    public static final int WIDTH = 10;
    public static final int HEIGHT = 10;



    int x, y;
    Tank.Direction dir;

    private boolean good ;
    private boolean live = true;
    private TankClient tc;

    public Missile(int x, int y, Tank.Direction dir) {
        this.x = x;
        this.y = y;
        this.dir = dir;
    }

    public Missile(int x, int y,boolean good, Tank.Direction dir, TankClient tc) {
        this(x, y, dir);
        this.good = good;
        this.tc = tc;
    }

    public void draw(Graphics g) {
        if (!live) {
            tc.missiles.remove(this);
            return;
        }

        Color c = g.getColor();
        g.setColor(Color.WHITE);
        g.fillOval(x, y, WIDTH, HEIGHT);
        g.setColor(c);

        move();
    }

    private void move() {


        switch (dir) {
            case L:  //左边
                x -= XSPEED;
                break;
            case LU: //左上
                x -= XSPEED;
                y -= YSPEED;
                break;
            case U:  //上
                y -= YSPEED;
                break;
            case RU: //右上
                x += XSPEED;
                y -= YSPEED;
                break;
            case R:  //右
                x += XSPEED;
                break;
            case RD: //右下
                x += XSPEED;
                y += YSPEED;
                break;
            case D: //下
                y += YSPEED;
                break;
            case LD: //左下
                x -= XSPEED;
                y += YSPEED;
                break;
            case STOP: //结束
                break;
        }
        if (x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) {
            live = false;
            tc.missiles.remove(this);
        }
    }

    public boolean isLive() {
        return live;
    }

    public Rectangle getRect() {
        return new Rectangle(x, y, WIDTH, HEIGHT);
    }

    public boolean hitTank(Tank t) {
        //判断好子弹和坏子弹才能打击
        if (this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.isGood()) {
            if (t.isGood()){
                t.setLife(t.getLife()-20);
               // if (t.getList() <=0) t.setLive(false);
            }
            t.setLive(false);
            this.live = false;
            Explode e = new Explode(x, y, tc);
            tc.explodes.add(e);
            return true;
        }
        return false;
    }
    public boolean hitTanks(List<Tank> tanks){
            for (int i=0;i<tanks.size();i++){
                if (hitTank(tanks.get(i))){
                    return true;
                }
            }
        return false;
    }

    public boolean hitWall(Wall w){
        if (this.live && this.getRect().intersects(w.getRect())){
            this.live = false;
            return true;
        }
        return false;
    }
}